<template>
  <div>
    <div class="container">
      <el-tree
        :data="data"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
      >
        <!-- 节点结构 -->
        <template v-slot="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="width: 100%; height: 40px"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manger">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 表示会接受子组件的事件 update:showDialog -->
    <add
      @updateDepartment="getDepartment"
      :showDialog.sync="showDialog"
      :currentNodeId="currentNodeId"
    ></add>
  </div>
</template>
<script>
import { getDepartment } from "@/api/department";
import add from "./components/add.vue";
import { transListToTreeData } from "@/utils";
export default {
  name: "Department",
  components: { add },
  data() {
    return {
      currentNodeId: null,
      showDialog: false,
      data: [],
      defaultProps: {
        children: "children",
        name: "name", //需要显示的字段名字
      },
    };
  },
  created() {
    this.getDepartment();
  },
  methods: {
    //
    async getDepartment() {
      const res = await getDepartment();
      this.data = transListToTreeData(res, 0);
    },
    handleCommand(type, id) {
      this.showDialog = true;
      this.currentNodeId = id;
    },
  },
};
</script>
<style scoped>
.container {
  padding: 30px 140px;
  font-size: 14px;
}
.manger {
  width: 50px;
  margin-right: 30px;
  display: inline-block;
}
</style>
